package br.ufmg.ubicomp.droidguide.common;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import br.ufmg.ubicomp.droidguide.eventservice.enums.EventType;
import br.ufmg.ubicomp.droidguide.eventservice.event.EventSource;
import br.ufmg.ubicomp.droidguide.eventservice.event.UserEvent;
import br.ufmg.ubicomp.droidguide.eventservice.management.EventManager;
import br.ufmg.ubicomp.droidguide.eventservice.management.EventService;

public class DroidGuideEntity {

	private String key;
	private String kind;
	private String data;
//	private Map<String, String> attributes = new HashMap<String, String>();
	private boolean forUpdate;
	
	

	public boolean isForUpdate() {
		return forUpdate;
	}

	public void setForUpdate(boolean forUpdate) {
		this.forUpdate = forUpdate;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getKind() {
		return kind;
	}

	public void setKind(String kind) {
		this.kind = kind;
	}

	public String getData() {
		return data;
	}

	public void setData(String data) {
		this.data = data;
	}	
	
//	public void addAttribute(String name, String value)
//	{
//		attributes.put(name, value);
//	}
//
//	public Map<String, String> getAttributes() {
//		return attributes;
//	}	
	
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("kind = ").append(kind).append("\r\n");
		sb.append("key = ").append(key).append("\r\n");
		sb.append("data = ").append(data).append("\r\n");
		sb.append("attributes: ").append("\r\n");
//		for (String k: attributes.keySet())
//		{
//			sb.append(k +" = ").append(attributes.get(k)).append("\r\n");
//		}
		
		return sb.toString();
	}

	protected void fireEntityChangeEvent(String source, String data) {
		if (forUpdate)
		{
			UserEvent event = EventManager.getInstance().createEvent(
					Calendar.getInstance(), EventType.PROFILE, source,
					EventSource.USER, data);
			// handle event on the server side
			EventService.getInstance().handleEvent(event);
		}
	}
}